/*******************************************************************/
/*    Copyright (c) 2008-2010 by Spatial Corp.                    */
/*    All rights reserved.                                         */
/*******************************************************************/

// EBomTreeNode.cpp: implementation of the EBomTreeNode class.
//
//////////////////////////////////////////////////////////////////////
//#include "stdafx.h"

#include "EBomTreeNode.h"

#ifdef XERCES_YES
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

EBomTreeNode::EBomTreeNode()
{
	m_sizeOfChild = 0;
	m_nodeName = NULL;
	m_fileName = NULL;
	m_positionMatrix = new double[16];
	m_positionMatrix[0] = 1.0;
	m_positionMatrix[1] = 0.0;
	m_positionMatrix[2] = 0.0;
	m_positionMatrix[3] = 0.0;
	m_positionMatrix[4] = 0.0;
	m_positionMatrix[5] = 1.0;
	m_positionMatrix[6] = 0.0;
	m_positionMatrix[7] = 0.0;
	m_positionMatrix[8] = 0.0;
	m_positionMatrix[9] = 0.0;
	m_positionMatrix[10] = 1.0;
	m_positionMatrix[11] = 0.0;
	m_positionMatrix[12] = 0.0;
	m_positionMatrix[13] = 0.0;
	m_positionMatrix[14] = 0.0;
	m_positionMatrix[15] = 1.0;

	m_ppChildList = NULL;
}

EBomTreeNode::EBomTreeNode( const int sizeOfChild, const TCHAR* nodeName/* = NULL*/, const TCHAR* fileName/* = NULL*/,const TCHAR* srcFileName/* = NULL*/,
	const TCHAR* refLinkId /* = NULL*/, const double* positionMatrix/* = NULL*/ )
	: m_sizeOfChild( sizeOfChild ), m_ppChildList( NULL ), m_nodeName( NULL ), m_fileName( NULL ), m_srcFileName( NULL ), m_refLinkId( NULL )
{
	m_positionMatrix = new double[16];
	if ( positionMatrix != NULL )
	{
		for ( int i = 0; i < 16; i++ )
		{
			m_positionMatrix[i] = positionMatrix[i];
		}
	}
	else
	{
		m_positionMatrix[0] = 1.0;
		m_positionMatrix[1] = 0.0;
		m_positionMatrix[2] = 0.0;
		m_positionMatrix[3] = 0.0;
		m_positionMatrix[4] = 0.0;
		m_positionMatrix[5] = 1.0;
		m_positionMatrix[6] = 0.0;
		m_positionMatrix[7] = 0.0;
		m_positionMatrix[8] = 0.0;
		m_positionMatrix[9] = 0.0;
		m_positionMatrix[10] = 1.0;
		m_positionMatrix[11] = 0.0;
		m_positionMatrix[12] = 0.0;
		m_positionMatrix[13] = 0.0;
		m_positionMatrix[14] = 0.0;
		m_positionMatrix[15] = 1.0;
	}

	if ( NULL == nodeName )
	{
		m_nodeName = new TCHAR[1];
		m_nodeName = L'\0';
	}
	else
	{
		size_t length = _tcslen( nodeName );
		m_nodeName = new TCHAR[length + 1];
		_tcscpy( m_nodeName, nodeName );
	}

	if ( NULL == fileName )
	{
		m_fileName = new TCHAR[1];
		m_fileName = L'\0';
	}
	else
	{
		size_t length = _tcslen( fileName );
		m_fileName = new TCHAR[length + 1];
		_tcscpy( m_fileName, fileName );
	}

	if ( NULL == m_srcFileName )
	{
		m_srcFileName = new TCHAR[1];
		m_srcFileName = L'\0';
	}
	else
	{
		size_t length = _tcslen( m_srcFileName );
		m_srcFileName = new TCHAR[length + 1];
		_tcscpy( m_srcFileName, srcFileName );
	}

	if ( NULL == refLinkId )
	{
		m_refLinkId = new TCHAR[1];
		m_refLinkId = L'\0';
	}
	else
	{
		size_t length = _tcslen( refLinkId );
		m_refLinkId = new TCHAR[length + 1];
		_tcscpy( m_refLinkId, refLinkId );
	}

	if ( m_sizeOfChild > 0 )
	{
		m_ppChildList = new EBomTreeNode * [m_sizeOfChild];
	}
}

EBomTreeNode::~EBomTreeNode()
{
	if ( NULL != m_nodeName )
	{
		delete[] m_nodeName;
		m_nodeName = NULL;
	}

	if ( NULL != m_fileName )
	{
		delete[] m_fileName;
		m_fileName = NULL;
	}

	if ( NULL != m_srcFileName )
	{
		delete[] m_srcFileName;
		m_srcFileName = NULL;
	}	
	
	if ( NULL != m_positionMatrix )
	{
		delete[] m_positionMatrix;
		m_positionMatrix = NULL;
	}

	if ( NULL != m_refLinkId )
	{
		delete[] m_refLinkId;
		m_refLinkId = NULL;
	}

	if ( m_sizeOfChild > 0 && NULL != m_ppChildList )
	{
		delete[] m_ppChildList;
		m_ppChildList = NULL;
	}
}

EBomTreeNode::EBomTreeNode( const EBomTreeNode& pNode )
{
	size_t length = _tcslen( pNode.m_nodeName );
	m_nodeName = new TCHAR[length + 1];
	_tcscpy( m_nodeName, pNode.m_nodeName );

	length = _tcslen( pNode.m_fileName );
	m_fileName = new TCHAR[length + 1];
	_tcscpy( m_fileName, pNode.m_fileName );

	length = _tcslen( pNode.m_srcFileName );
	m_srcFileName = new TCHAR[length + 1];
	_tcscpy( m_srcFileName, pNode.m_srcFileName );

	length = _tcslen( pNode.m_refLinkId );
	m_refLinkId = new TCHAR[length + 1];
	_tcscpy( m_refLinkId, pNode.m_refLinkId );

	m_sizeOfChild = pNode.m_sizeOfChild;
	m_ppChildList = new EBomTreeNode * [m_sizeOfChild];
}

EBomTreeNode& EBomTreeNode::operator=( const EBomTreeNode& pNode )
{
	if ( this == &pNode )
	{
		return *this;
	}

	if ( NULL != m_positionMatrix )
	{
		delete[] m_positionMatrix;
		m_positionMatrix = NULL;
	}

	if ( NULL != m_nodeName )
	{
		delete[] m_nodeName;
		m_nodeName = NULL;
	}

	if ( NULL != m_fileName )
	{
		delete[] m_fileName;
		m_fileName = NULL;
	}

	if ( NULL != m_srcFileName )
	{
		delete[] m_srcFileName;
		m_srcFileName = NULL;
	}

	if ( NULL != m_refLinkId )
	{
		delete[] m_refLinkId;
		m_refLinkId = NULL;
	}

	if ( NULL != m_ppChildList )
	{
		delete[] m_ppChildList;
		m_ppChildList = NULL;
	}

	size_t length = _tcslen( pNode.m_nodeName );
	m_nodeName = new TCHAR[length + 1];
	_tcscpy( m_nodeName, pNode.m_nodeName );

	length = _tcslen( pNode.m_fileName );
	m_fileName = new TCHAR[length + 1];
	_tcscpy( m_fileName, pNode.m_fileName );

	length = _tcslen( pNode.m_srcFileName );
	m_srcFileName = new TCHAR[length + 1];
	_tcscpy( m_srcFileName, pNode.m_srcFileName );

	length = _tcslen( pNode.m_refLinkId );
	m_refLinkId = new TCHAR[length + 1];
	_tcscpy( m_refLinkId, pNode.m_refLinkId );

	m_sizeOfChild = pNode.m_sizeOfChild;
	m_ppChildList = new EBomTreeNode * [m_sizeOfChild];

	return *this;
}

void EBomTreeNode::SetFileName( TCHAR* pNewFileName )
{
	if ( NULL != m_fileName )
	{
		delete[] m_fileName;
		m_fileName = NULL;
	}

	size_t length = _tcslen( pNewFileName );
	m_fileName = new TCHAR[length + 1];
	_tcscpy( m_fileName, pNewFileName );
}

void EBomTreeNode::SetSrcFileName( TCHAR* pNewSrcFileName )
{
	if ( NULL != m_srcFileName )
	{
		delete[] m_srcFileName;
		m_srcFileName = NULL;
	}

	size_t length = _tcslen( pNewSrcFileName );
	m_srcFileName = new TCHAR[length + 1];
	_tcscpy( m_srcFileName, pNewSrcFileName );
}

const TCHAR* EBomTreeNode::GetRefLinkId() const
{
	return m_refLinkId;
}

EBomTreeNode** EBomTreeNode::GetChilds() const
{
	return m_ppChildList;
}

const TCHAR* EBomTreeNode::GetSrcFileName() const
{
	return m_srcFileName;
}

const int EBomTreeNode::GetSize() const
{
	return m_sizeOfChild;
}

const TCHAR* EBomTreeNode::GetNodeName() const
{
	return m_nodeName;
}

const double* EBomTreeNode::GetPositionMatrix() const
{
	return m_positionMatrix;
}

const TCHAR* EBomTreeNode::GetFileName() const
{
	return m_fileName;
}
#endif